<template>
    <div>
        <el-page-header content="个人中心" icon="" title="企业门户管理系统"></el-page-header>
        <el-card>
            <el-table :data="tableData" style="width: 100%">
                <el-table-column prop="username" label="用户名" />
                <el-table-column prop="avatar" label="头像">
                    <template #default="scope">
                        <div v-if="scope.row.avatar">
                            <el-avatar :size="50" shape="circle" :src="'http://localhost:3000' + scope.row.avatar"
                                fit="fill"></el-avatar>
                        </div>
                        <div v-else>
                            <el-avatar :size="50" shape="circle"
                                src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png"
                                fit="fill"></el-avatar>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column prop="role" label="角色">
                    <template #default="scope">
                        <el-tag v-if="scope.row.role === 1">管理员</el-tag>
                        <el-tag v-else type="success">编辑</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="操作">
                    <template #default="scope">
                        <el-button size="small" type="primary" @click="handleEdit(scope.row)">编辑</el-button>
                        <el-popconfirm title="你确认删除吗？" confirmButtonText="确定" cancelButtonText="取消"
                            @confirm="handleDelete(scope.row)">
                            <template #reference>
                                <el-button size="small" type="danger">删除</el-button>
                            </template>
                        </el-popconfirm>

                    </template>
                </el-table-column>
            </el-table>
        </el-card>

        <el-dialog v-model="dialogVisible" title="编辑用户" width="30%">
            <el-form ref="userFormRef" :model="userForm" :rules="userFormRules" label-width="80px" class="demo-ruleForm"
                status-icon>
                <el-form-item label="用户名" prop="username">
                    <el-input v-model="userForm.username" placeholder="请输入用户名" />
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input type="password" v-model="userForm.password" placeholder="请输入密码" show-password />
                </el-form-item>
                <el-form-item label="权限" prop="role">
                    <el-select v-model="userForm.role" placeholder="Select" style="width: 100%;">
                        <el-option v-for="item in roleOptions" :key="item.value" :label="item.label" :value="item.value" />
                    </el-select>
                </el-form-item>
                <el-form-item label="性别" prop="gender">
                    <el-select v-model="userForm.gender" placeholder="Select" style="width: 100%;">
                        <el-option v-for="item in genderOptions" :key="item.value" :label="item.label"
                            :value="item.value" />
                    </el-select>
                </el-form-item>
                <el-form-item label="个人简介" prop="introduction">
                    <el-input type="textarea" v-model="userForm.introduction" resize="none" maxlength="50"
                        placeholder="请输入个人简介" show-word-limit />
                </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="dialogVisible = false">取消</el-button>
                    <el-button type="primary" @click="handleEditConfirm">
                        确认
                    </el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>



<script setup>
import { ref, reactive, onMounted } from 'vue';
import { ElMessage } from 'element-plus'
import axios from 'axios';
const tableData = ref([])
const dialogVisible = ref(false)
// 关联userForm表单ref
const userFormRef = ref()
// 双向绑定userForm表单数据
let userForm = reactive({
    username: "",
    password: "",
    role: 2,// 1为管理员 2为编辑
    gender: 0,// 0为保密，1为男，2为女
    introduction: "",
})
// 定义规则校验
const userFormRules = reactive({
    username: [
        { required: true, message: '请输入你的名字', trigger: 'blur' },
    ],
    password: [
        { required: true, message: '请输入你的密码', trigger: 'blur' },
    ],
    role: [
        { required: true, message: '请选择权限', trigger: 'blur' },
    ],
    gender: [
        { required: true, message: '请选择你的性别', trigger: 'blur' },
    ],
    introduction: [
        { required: true, message: '请输入个人简介', trigger: 'blur' },
    ],

})
// 权限选择字段
const roleOptions = [
    {
        label: "管理员",
        value: 1
    },
    {
        label: "编辑",
        value: 2
    },
]
// 性别选择字段
const genderOptions = [
    {
        label: "保密",
        value: 0
    },
    {
        label: "男",
        value: 1
    },
    {
        label: "女",
        value: 2
    },
]
onMounted(() => {
    getTableData()
})
const getTableData = async () => {
    const res = await axios.get("/adminapi/user/list")
    // console.log(res.data);
    tableData.value = res.data.data
}

const handleEdit = async (data) => {
    // console.log(data);
    const res = await axios.get(`/adminapi/user/list/${data._id}`)
    // console.log(res);
    Object.assign(userForm, res.data.data[0])
    dialogVisible.value = true
}
const handleDelete = async (data) => {
    // console.log(data);
    const result = await axios.delete(`/adminapi/user/list/${data._id}`)
    console.log(result);
    if (result.data.ActionType === 'OK') {
        ElMessage.success("删除成功")
        getTableData()
    } else {
        ElMessage.error(result.errorMsg)
    }
}

const handleEditConfirm = () => {
    userFormRef.value.validate(async (valid) => {
        if (valid) {
            // 更新后台
            const res = await axios.put(`/adminapi/user/list/${userForm._id}`, userForm)
            // console.log(res);
            if (res.data.ActionType === 'OK') {
                ElMessage.success("更新成功")
                dialogVisible.value = false
                getTableData()
            } else {
                ElMessage.error(res.data.errorMsg)
            }

        }
    })

}


</script>

<style lang="scss" scoped>
.el-card {
    margin-top: 50px;
}
</style>